package com.cmic.rcs.emoji.controller.app;

import com.cmic.rcs.emoji.common.UserContext;

import com.cmic.rcs.emoji.entity.pojo.TEnterpriseMember;
import com.cmic.rcs.emoji.entity.vo.EnterpriseAPPMemberVO;
import com.cmic.rcs.emoji.mapper.TEnterpriseMemberMapper;
import com.cmic.rcs.emoji.util.EncryptionUtils;
import com.cmic.rcs.enhance.message.g5.core.common.Response;
import com.cmic.rcs.enhance.message.g5.core.common.ResponseData;
import com.cmic.rcs.enhance.message.g5.core.common.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * APP端个人中心控制器
 * author:孙高明
 * date:2025-10-15
 */
@RestController
@RequestMapping("/api/app/personalCenter")
@Slf4j
public class PersonalCenterController {
    @Autowired
    private TEnterpriseMemberMapper enterpriseMemberMapper;



    /**
     * 获取当前用户信息
     * @return
     */
    @PostMapping("/getEnterpriseMemberInfo")
    public Response getEnterpriseMemberInfo() {
        //1/获取当前用户信息
        String phone = UserContext.getUserDto().getUsername();
        //2.根据手机号查询用户信息
        TEnterpriseMember tEnterpriseMember = enterpriseMemberMapper.selectByEncryptedPhone(phone);
        if (tEnterpriseMember == null){
            return Response.Error(ReturnCodeEnum.NULL_EXCEPTION, "用户不存在");
        }
        //3.返回用户信息
        EnterpriseAPPMemberVO memberVO = new EnterpriseAPPMemberVO();
        memberVO.setMemberName(tEnterpriseMember.getMemberName());
        memberVO.setMemberImg(tEnterpriseMember.getMemberImg());
        //4.对phone进行加密138****5283
        String memberAccount = EncryptionUtils.encryptMobile(phone.substring(0,3) + "****" + phone.substring(7));
        memberVO.setMemberAccount(memberAccount);
        log.info("memberAccount:{}", memberAccount);
        return ResponseData.OK(memberVO);
    }

    /**
     * 用户注销
     * @return
     */
    @PostMapping("logout")
    public Response logout() {
        log.info("用户注销请求");
        try {
            // 获取当前用户名（手机号）
            String username = UserContext.getUserDto() != null ? UserContext.getUserDto().getUsername() : null;


            // 清除用户上下文
            UserContext.remove();

            // 返回成功信息
            return ResponseData.OK("注销成功");
        } catch (Exception e) {
            log.error("用户注销异常: {}", e.getMessage(), e);
            return ResponseData.Error(ReturnCodeEnum.SYSTEM_ERROR, "注销失败，请重试");
        }
    }

}
